package com.techsoul.module.pms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.constant.ApiConstants;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.pms.mapper.YunheFireOrEventMesgMapper;
import com.techsoul.module.pms.pojo.entity.YunheFireOrEventMesg;
import com.techsoul.module.pms.pojo.param.YunheFireOrEventMesgSaveParam;
import com.techsoul.module.pms.pojo.param.YunheFireOrEventMesgSearchParam;
import com.techsoul.module.pms.pojo.vo.YunheFireOrEventMesgVo;
import com.techsoul.module.pms.service.IYunheFireOrEventMesgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * mesg Service实现
 * 
 * @author liuen
 * @date 2021-09-23
 */
@Service
@Transactional(readOnly = true)
public class YunheFireOrEventMesgServiceImpl implements IYunheFireOrEventMesgService {

    @Autowired
    private IdWorker idWorker;



    @Autowired
    private YunheFireOrEventMesgMapper mapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(YunheFireOrEventMesgSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        YunheFireOrEventMesg entity = new YunheFireOrEventMesg();
        BeanUtils.copyProperties(params, entity);
        if(!StringUtils.isNull(entity.getVideoPath())){
            String vp = entity.getVideoPath();
         //   vp = "http://115.29.199.1:81/".concat(vp);
            entity.setVideoPath(vp);
        }
        if(!StringUtils.isNull(entity.getPicPath())){
            String pp = entity.getPicPath();
         //   pp = "http://115.29.199.1:81/".concat(pp);
            entity.setPicPath(pp);
        }
        entity.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insertAlert(YunheFireOrEventMesgSaveParam params) {
        YunheFireOrEventMesg entity = new YunheFireOrEventMesg();
        BeanUtils.copyProperties(params, entity);
        entity.setStatus("created");
        entity.setId(idWorker.nextId() + "").preInsert(params.getCreateBy());
        this.mapper.insert(entity);
        return entity.getId();
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }


    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(YunheFireOrEventMesgSaveParam params) {
        YunheFireOrEventMesg entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        //BeanUtils.copyProperties(params, entityFromDb);
        if(!StringUtils.isNull(params.getNextProcessDepartment())){
            entityFromDb.setNextProcessDepartment(params.getNextProcessDepartment());
            entityFromDb.setNextProcessDept(params.getNextProcessDept());
            entityFromDb.setNextProcessDepartmentSentYn("Y");
        }
        if(!StringUtils.isNull(params.getCheckPerson())){
            entityFromDb.setCheckPerson(params.getCheckPerson());

        }
        if(!StringUtils.isNull(params.getCheckNote())){
            entityFromDb.setCheckNote(params.getCheckNote());

        }
        if(!StringUtils.isNull(params.getProveNote())){
            entityFromDb.setProveNote(params.getProveNote());

        }
        if(!StringUtils.isNull(params.getProvePerson())){
            entityFromDb.setProvePerson(params.getProvePerson());

        }

        entityFromDb.setStatus(params.getStatus());
        entityFromDb.preUpdate(sysUser.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public YunheFireOrEventMesgVo findById(String id) {
        YunheFireOrEventMesgVo yunheFireOrEventMesgVo = this.mapper.findById(id);
        if (ApiConstants.N.equals(yunheFireOrEventMesgVo.getIsRead())) {
            YunheFireOrEventMesgSaveParam yunheFireOrEventMesgSaveParam = new YunheFireOrEventMesgSaveParam();
            yunheFireOrEventMesgSaveParam.setIsRead(ApiConstants.Y)
                    .setId(yunheFireOrEventMesgVo.getId());
            this.update(yunheFireOrEventMesgSaveParam);
        }
        return yunheFireOrEventMesgVo;
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<YunheFireOrEventMesgVo> findAll(YunheFireOrEventMesgSearchParam params) {
        return this.mapper.findAll(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<YunheFireOrEventMesgVo> findAll(YunheFireOrEventMesgSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }

}
